<?php

namespace Common\Model;
use Think\Model;
use Common\Model\UserCarModel;

class CarLoginLogic extends Model {

	protected $tableName = 'user_car';

  private $carModel;

  /**
   * 120端用户登陆
   */
 	public function login($account,$password){
    $this->carModel = new UserCarModel;
    if($this->carModel->isPhoneExist($account)){
      $this->carModel->where(['phone'=>$account])->find();
      return $this->checkPasswordAndRecordToken($password);
    }
    if($this->carModel->isPalteNumberExist($account)){
      $this->carModel->where(['plate_number'=>$account])->find();
      return $this->checkPasswordAndRecordToken($password);
    }

    return '手机or车牌号码不存在';
  }

  /**
   * 检查密码，处理token
   */
  public function checkPasswordAndRecordToken($password){
    if($this->carModel->password != $this->carModel->getPassword($password)){
      return '密码不正确，请重试';
    }
    //检查用户状态
     if($this->carModel->status == UserCarModel::STATUS_INIT){
      return '用户审核中，请练习管理员后重试';
     }
     if($this->carModel->status == UserCarModel::STATUS_NOT_PASS){
      return '抱歉，您没有通过审核';
     }

     return true;
  }

  /**
   * 获取用户登陆token
   */
  public function getLoginToken(){
    return $this->carModel->getToken();
  }

    /**
     * 获取用户登录id
     * @return int
     */
    public function getUserId(){
        return $this->carModel->id;
    }

}